<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class article_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    function index()
    {
        $cid = isset($_GET['cid']) ? $_GET['cid'] : '';
        $key = isset($_GET['key']) ? $_GET['key'] : '';
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $category = $this->module('article')->category_list();
        $wsql = '';
        if (!empty($cid)) {
            $wsql .= ' cid in (' . $this->module('article')->category_in($cid) . ') and ';
        }
        if (!empty($key)) {
            $wsql .= " title like '%" . $key . "%' and ";
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $query = $this->db->get_all("select * from " . table('article') . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($query) {
            foreach ($query as $k => $v) {
                $v['cname'] = @$category[$v['cid']]['name'];
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $total = $this->db->get_one("select count(*) as total from " . table('article') . $wsql);
            $data['list'] = $list;
            if ($page_size < $total['total']) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function edit()
    {
        $id = !empty($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('article') . " where id = " . $id);
            $row['content'] = htmlspecialchars($row['content']);
            $this->assign('row', $row);
        }
        $this->assign('cats_tree', $this->category_tree_list());
        $this->display();
    }
    function save()
    {
        if ($_POST) {
            $setsql['cid'] = P('cid', '分类不能为空');
            $setsql['title'] = P('title', '标题不能为空');
            $setsql['content'] = P('content', '内容不能为空');
            $setsql['simg'] = P('simg');
            $setsql['desc'] = P('desc');
            $setsql['is_msg'] = P('is_msg', '', '1');
            $setsql['click'] = P('click', '', '0');
            $setsql['like'] = P('like', '', '0');
            $setsql['from'] = P('from');
            $setsql['addtime'] = time();
            $id = P("id", false);
            if ($id) {
                $query = $this->db->update(table('article'), $setsql, "id = " . $id);
            } else {
                $query = $this->db->add(table('article'), $setsql);
            }
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        }
    }
    function del()
    {
        $id = P('id', '提交编号错误');
        if ($id) {
            $query = $this->db->delete(table('article'), 'id in(' . $id . ')');
            if ($query) {
                right_json();
            } else {
                error_json('删除失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    function upload_img()
    {
        $file_url = upload::store('data/article/');
        header('Content-type: text/html; charset=UTF-8');
        echo json_encode(array('error' => 0, 'url' => $file_url));
        exit;
    }
    public function ajax_article_pass()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : exit('ID error');
        $this->db->query("update " . table('article') . " set `pass` = (`pass` + 1)%2 where id = " . $id . " limit 1");
        $row = $this->db->get_one("select * from " . table('article') . " where id = " . $id . " limit 1");
        echo $row['pass'];
    }
    public function category()
    {
        $this->assign('data', $this->category_tree_list());
        $this->display();
    }
    public function category_edit()
    {
        $cid = isset($_GET['cid']) ? $_GET['cid'] : false;
        if ($cid) {
            $this->assign('row', $this->category_one($cid));
        }
        $this->assign('cats_list', $this->category_tree_list());
        $this->display();
    }
    public function category_save()
    {
        $data['pcid'] = P('pcid', '');
        $data['name'] = P('name', '分类名不能为空');
        $data['keywords'] = P('keywords');
        $data['describe'] = P('describe');
        $data['status'] = P('status', '', '0');
        $data['addtime'] = time();
        $cid = P('cid', '', false);
        if ($cid) {
            $query = $this->db->update(table("article_category"), $data, 'cid = ' . $cid);
        } else {
            $query = $this->db->add(table("article_category"), $data);
        }
        if ($query) {
            right_json();
        } else {
            error_json('保存失败');
        }
    }
    public function category_del()
    {
        $cid = P('id', '提交编号错误');
        $cid = $this->module("article")->category_in($cid);
        $num = $this->db->get_one("select count(*) as total from " . table('article') . " where cid in (" . $cid . ")");
        if ($num['total'] > 0) {
            error_json('分类下还有内容，无法删除');
        } else {
            $query = $this->db->delete(table('article_category'), 'cid = ' . $cid);
            if ($query) {
                right_json();
            } else {
                error_json('删除失败');
            }
        }
    }
    function category_one($cid)
    {
        return $this->db->get_one("select * from " . table('article_category') . " where cid = " . $cid);
    }
    public function category_tree_list()
    {
        $query = $this->db->get_all("select * from " . table('article_category') . " order by px desc");
        if ($query) {
            foreach ($query as $v) {
                $v['addtime'] = _date($v['addtime']);
                $data[] = $v;
            }
            return $this->create_tree_list(list_to_tree($data));
        } else {
            return false;
        }
    }
    private function create_tree_list($dataarray, $num = 0)
    {
        static $cats_list;
        $pre = "";
        for ($i = 0; $i < $num; $i++) {
            $pre .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        $num += 1;
        foreach ($dataarray as $k => $v) {
            $is_frist = $num == 1 ? '' : '|-- ';
            $v['name'] = $pre . $is_frist . $v['name'];
            if (isset($v['_child'])) {
                $cats_list[] = $v;
                $this->create_tree_list($v['_child'], $num);
            } else {
                $cats_list[] = $v;
            }
        }
        return $cats_list;
    }
    function comment()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $list = $this->db->get_all("select c.*,c.id as comment_id,a.id,a.title,u.uid,u.nick from " . table('article_comment') . " as c left join " . table('article') . " as a on a.id = c.aid left join " . table("member") . " as u on c.uid = u.uid order by comment_id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        $data['list'] = $list;
        $total = $this->db->get_one("select count(*) as total from " . table('article_comment'));
        if ($page_size < $total['total']) {
            $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
        }
        $this->assign('data', $data);
        $this->display();
    }
    public function comment_del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('ID error');
        $query = $this->db->delete(table('article_comment'), 'id = ' . $id);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
    public function comment_ajax_pass()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : exit('ID error');
        $this->db->query("update " . table('article_comment') . " set `pass` = (`pass` + 1)%2 where id = " . $id . " limit 1");
        $row = $this->db->get_one("select * from " . table('article_comment') . " where id = " . $id . " limit 1");
        echo $row['pass'];
    }
}